/* QAD_ProductStructureCostReport.p - Report Proxy Template                                       */
/* Copyright 1986-2010 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy !!old.p!! for use by QAD Reporting Framework.        */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table tt_ps_mstr no-undo like ps_mstr
   field lvl                  as   character
   field um                   like pt_mstr.pt_um
   field batch_qty            like bom_mstr.bom_batch
   field batch_um             like bom_mstr.bom_batch_um
   field des1 as              like pt_mstr.desc1
   field des2                 as   character
   field des3                 as   character
   field des4                 as   character
   field des5                 as   character
   field des6                 as   character
   field des7                 as   character
   field des8                 as   character
   field des9                 as   character
   field des10                as   character
   field id_group_by_parent   as   integer
   field id_sequence_in_group as   integer
   field unique_id            as   integer
.

define temp-table tt_sct_det no-undo like sct_det
   field ref_unique_id as integer
.

define temp-table tt_spt_det no-undo like spt_det
   field catelog       as character
   field ref_unique_id as integer
.

define temp-table ttReportOptions no-undo
   field newpage      like mfc_ctrl.mfc_logical
   field catelm       like mfc_ctrl.mfc_logical
   field formula_only like mfc_ctrl.mfc_logical
.

define temp-table ttReportConditions no-undo
   field part      like pt_mstr.pt_part
   field numlevels as   integer
   field eff_date  as   date
   field site      like si_mstr.si_site
   field csset     like sct_det.sct_sim
.

define dataset dsReportResults for tt_ps_mstr, tt_sct_det, tt_spt_det, ttReportOptions, ttReportConditions.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS as handle    no-undo.

empty temp-table tt_ps_mstr         no-error.
empty temp-table tt_sct_det         no-error.
empty temp-table tt_spt_det         no-error.
empty temp-table ttReportOptions    no-error.
empty temp-table ttReportConditions no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_ProductStructureCostReport.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   /*
   run SetFieldMetaParameter in ipReportHandle(
         "ttCoByProductsCostRpt", "si_site", "DefaultValue", site).
   */

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString   as character no-undo.
   define variable hMainQuery    as handle    no-undo.
   define query    MainQuery     for so_mstr.

   assign
      hMainQuery           = query MainQuery:handle.

   queryString = "for each so_mstr no-lock where so_domain = "
                  + quoter(global_domain).

   run AddAllConditions in reportHandle
      ("so_mstr", input-output queryString).

   queryString = queryString + ":".

   hMainQuery:query-prepare(queryString).
   hMainQuery:query-open().
   hMainQuery:get-next().

   repeat while not hMainQuery:query-off-end:

      hMainQuery:get-next().
   end. /* REPEAT QUERY */

   hMainQuery:query-close().
   delete object hMainQuery.
   phReportResults = dataset dsReportResults:handle.

END PROCEDURE.
